<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
          <el-form-item prop="desc" :label="$t('Video.desc')">
            <el-input v-model="state.queryForm.desc" :placeholder="$t('Video.inputDescTip')" clearable
              @clear="getDataList" />
          </el-form-item>
          <el-form-item :label="$t('Video.state')" prop="state">
            <el-select v-model="state.queryForm.state" :placeholder="$t('Video.inputStateTip')" clearable filterable
              @clear="getDataList">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_video_state"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'bizs_Video_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary" v-auth="'bizs_Video_del'"
            @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'bizs_Video_export'" @exportExcel="exportExcel"
            class="ml10 mr20" style="float: right;" @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle" @selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" />
        <!--<el-table-column type="index" label="#" width="40" />-->
        <el-table-column prop="url" :label="$t('Video.url')" show-overflow-tooltip />
        <el-table-column prop="desc" :label="$t('Video.desc')" show-overflow-tooltip />
        <el-table-column prop="type" :label="$t('Video.type')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="biz_video_type" :value="scope.row.type"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="praiseCount" :label="$t('Video.praiseCount')" show-overflow-tooltip />
        <el-table-column prop="praiseCount" :label="$t('Video.videoPreview')" show-overflow-tooltip>
          <template #default="{ row }">
            <!-- <img style="height:100px;width:100px" :src="row.url" alt=""> -->
            <div class="flex-center-center">

              <video style="width:100px;height: 100px;" :src="$getFileUri(row.url)" @click="preView(row)"></video>
            </div>
            <!-- <el-button text type="primary" @click="preView(row)">预览视频</el-button> -->
          </template>
        </el-table-column>
        <el-table-column prop="commentCount" :label="$t('Video.commentCount')" show-overflow-tooltip />
        <el-table-column prop="favoriteCount" :label="$t('Video.favoriteCount')" show-overflow-tooltip />
        <el-table-column prop="shareCount" :label="$t('Video.shareCount')" show-overflow-tooltip />
        <el-table-column prop="playCount" :label="$t('Video.playCount')" show-overflow-tooltip />
        <el-table-column prop="isSale" :label="$t('Video.isSale')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="yes_no_bool" :value="scope.row.isSale"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="isTop" :label="$t('Video.isTop')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="yes_no_bool" :value="scope.row.isTop"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="state" :label="$t('Video.state')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="biz_video_state" :value="scope.row.state"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" :label="$t('Video.createTime')" show-overflow-tooltip />
        <el-table-column :label="$t('common.action')" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'bizs_Video_edit'"
              @click="formDialogRef.openDialog(scope.row.id)">{{ $t('common.editBtn') }}</el-button>
            <el-button icon="delete" text type="primary" v-auth="'bizs_Video_del'"
              @click="handleDelete([scope.row.id])">{{ $t('common.delBtn') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
      <VideoPlayer ref="VideoPlayerREF" />
    </div>
    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemVideo">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { getPage, delObjs } from "/@/api/bizs/Video";
import VideoPlayer from "/@/components/biz/video-player.vue"
// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典
import { useDict } from '/@/hooks/dict';
import {getFileUri} from "/@/utils/util";

const { biz_video_type, yes_no_bool, biz_video_state } = useDict('biz_video_type', 'yes_no_bool', 'biz_video_state')
// 定义变量内容
const formDialogRef = ref()
const VideoPlayerREF = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: getPage
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/bizs/video/export', Object.assign(state.queryForm, { ids: selectObjs }), 'Video.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('common.delConfirmText'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('common.delSuccessText'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};

interface VideoItem {
  createBy: string;
  createTime: string;
  updateBy: string;
  updateTime: string;
  search?: any;
  rangeTime?: any;
  id: string;
  uid: string;
  shopId: string;
  width: number;
  height: number;
  url: string;
  desc: string;
  type: number;
  praiseCount: number;
  commentCount: number;
  favoriteCount: number;
  shareCount: number;
  playCount: number;
  isSale: boolean;
  isTop: boolean;
  state: number;
  baseFavoriteCount: number;
  basePraiseCount: number;
  topTime?: any;
  delFlag: boolean;
  videoProdRecommendList?: any;
  like: boolean;
}
const preView = (row: VideoItem) => {
  VideoPlayerREF.value.visible = true
  VideoPlayerREF.value.options.src = getFileUri(row.url)

  nextTick(() => {
    VideoPlayerREF.value.playFn()
  })
}



</script>

<style lang="scss" scoped>
.flex-center-center {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
